const pool = require('../config/db');
const {decryptWithPrivateKey} = require('../utils/crypto');
const {encryptAES, decryptAES} = require('../utils/aes');
const _ = require('lodash');
const {calculate} = require('./destinationJudgement.js');

// 获取所有数据
exports.getAllData = async (req, res) => {
  try {
    const [rows] = await pool.query('SELECT * FROM pe_direction');
    res.json(rows);
  } catch (error) {
    console.error('Database error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
};

// 按种类查询数据
exports.getDataByType = async (req, res) => {
  const { type } = req.query;
  if (!type) {
    return res.status(400).json({ error: 'Type is required' });
  }
  try {
    const [rows] = await pool.query('SELECT * FROM pe_direction WHERE n_type_news = ?', [type]);
    res.json(rows);
  } catch (error) {
    console.error('Database error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
};

exports.postDataByType = async (req, res) => {
  const { encryptedAESKey, encryptedData } = req.body;
  if (!encryptedData) {
    return res.status(400).json({ error: "Missing encrypted data" });
  }
  try {
    // console.log(encryptedAESKey,encryptedData);
    const type = JSON.parse(decryptWithPrivateKey(encryptedData));
    const aesKey = JSON.parse(decryptWithPrivateKey(encryptedAESKey));
    console.log(`type: ${type}`);
    console.log('AESKey: ', aesKey);
    if (!type) {
      return res.status(400).json({ error: 'Type is required' });
    }
    const [rows] = await pool.query('SELECT * FROM pe_direction WHERE n_type_news = ?', [type]);
    // 使用 AES 加密返回数据
    const result = encryptAES(rows, aesKey);
    // console.log(result.iv, result.ciphertext);
    const correct = decryptAES(result.ciphertext, aesKey, result.iv);
    if (_.isEqual(correct, rows)) {
      console.log("correct");
    } else {
      console.log("incorrect");
    }
    
    res.json({ encryptedData: result.ciphertext , iv: result.iv});
  } catch (error) {
    console.error('Database error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
};

exports.postCalculateData = async (req,res) =>{
	const { encryptedAESKey, encryptedData } = req.body;
	if (!encryptedData) {
	  return res.status(400).json({ error: "Missing encrypted data" });
	}
	try {
	  // console.log(encryptedAESKey,encryptedData);
	  const data = JSON.parse(decryptWithPrivateKey(encryptedData));
	  const aesKey = JSON.parse(decryptWithPrivateKey(encryptedAESKey));
	  const destination = data[0];
	  const weight = data[1];
	  console.log('destination, weight:', destination, weight);
	  console.log('AESKey: ', aesKey);
	  const rows = await calculate(destination, weight);
	  // 使用 AES 加密返回数据
	  const result = encryptAES(rows, aesKey);
	  // console.log(result.iv, result.ciphertext);
	  const correct = decryptAES(result.ciphertext, aesKey, result.iv);
	  if (_.isEqual(correct, rows)) {
	    console.log("correct");
	  } else {
	    console.log("incorrect");
	  }
	  
	  res.json({ encryptedData: result.ciphertext , iv: result.iv});
	} catch (error) {
	  console.error('Database error:', error);
	  res.status(500).json({ error: 'Internal server error' });
	}
}